package com.newgoldcurrency.view.goldButton;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

import com.newgoldcurrency.utils.Tools;

public abstract class LoadingRenderer {
    private static final long ANIMATION_DURATION = 1333;
    private static final float DEFAULT_SIZE = 30;
    private final ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener = animation -> {
        computeRender((float) animation.getAnimatedValue());
        invalidateSelf();
    };
    protected final Rect mBounds = new Rect();
    private Drawable.Callback mCallback;
    private ValueAnimator mRenderAnimator;
    protected long mDuration;
    protected float mWidth;
    protected float mHeight;
    public LoadingRenderer(Context context) {
        initParams(context);
        setupAnimators();
    }
    @Deprecated
    protected void draw(Canvas canvas, Rect bounds) {
    }
    protected void draw(Canvas canvas) {
        draw(canvas, mBounds);
    }
    protected abstract void computeRender(float renderProgress);
    protected abstract void setAlpha(int alpha);
    protected abstract void setColorFilter(ColorFilter cf);
    protected abstract void reset();

    void start() {
        reset();
        mRenderAnimator.addUpdateListener(mAnimatorUpdateListener);
        mRenderAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mRenderAnimator.setDuration(mDuration);
        mRenderAnimator.start();
    }
    void stop() {
        mRenderAnimator.removeUpdateListener(mAnimatorUpdateListener);
        mRenderAnimator.setRepeatCount(0);
        mRenderAnimator.setDuration(0);
        mRenderAnimator.end();
    }
    boolean isRunning() {
        return mRenderAnimator.isRunning();
    }
    void setCallback(Drawable.Callback callback) {
        this.mCallback = callback;
    }
    void setBounds(Rect bounds) {
        mBounds.set(bounds);
    }
    private void initParams(Context context) {
        mWidth = Tools.dip2px(context, DEFAULT_SIZE);
        mHeight = Tools.dip2px(context, DEFAULT_SIZE);
        mDuration = ANIMATION_DURATION;
    }
    @SuppressLint("WrongConstant")
    private void setupAnimators() {
        mRenderAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
        mRenderAnimator.setRepeatCount(Animation.INFINITE);
        mRenderAnimator.setRepeatMode(Animation.RESTART);
        mRenderAnimator.setDuration(mDuration);
        mRenderAnimator.setInterpolator(new LinearInterpolator());
        mRenderAnimator.addUpdateListener(mAnimatorUpdateListener);
    }
    private void invalidateSelf() {
        mCallback.invalidateDrawable(null);
    }
}